package com.yggdrasil;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.Timer;

import com.yggdrasil.graphics.CharView;
import com.yggdrasil.graphics.Screen;
import com.yggdrasil.graphics.Tile;
import com.yggdrasil.graphics.WorldView;
import com.yggdrasil.graphics.input.KeyHandler;
import com.yggdrasil.world.Enemy;
import com.yggdrasil.world.NPC;
import com.yggdrasil.world.Player;

public class Yggdrasil {

	Tile[] tiles = new Tile[1280 * 600];
	KeyHandler kh;
	Player p;
	ArrayList<NPC> npcs;
	ArrayList<Enemy> enemies;

	public Yggdrasil() {
		for (int i = 0; i < tiles.length; i++) {
			tiles[i] = new Tile(Tile.TYPE.GRASS);
		}
		kh = new KeyHandler();
		p = new Player(100, 100);

		WorldView world = new WorldView(tiles, kh);
		world.addDrawable(p);
		world.addDrawable(new Enemy(500, 520, 40));
		world.addDrawable(new NPC(500, 500, 1000));
		CharView cv = new CharView(p);
		Screen s = new Screen(world, cv, kh);
		s.add(world, BorderLayout.NORTH);
		s.add(cv, BorderLayout.SOUTH);

		Thread display = new Thread(s);
		display.start();

		Timer t = new Timer(20, new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				tick();
			}

		});
		t.start();
	}

	public void tick() {
		if (kh.right && !kh.left)
			p.addToX(10);
		else if (!kh.right && kh.left)
			p.addToX(-10);
		if (kh.up && !kh.down)
			p.addToY(-10);
		else if (!kh.up && kh.down)
			p.addToY(10);
	}

	public static void main(String[] args) {
		new Yggdrasil();
		// TESTING SHORTCUT
	}

}
